package com.bookmanager.www.mapper;

import com.bookmanager.www.entity.FlUser;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/12/09/13:56
 * @Description:
 */
@Component
public class FlMongoTestMapper {

    @Resource
    private MongoTemplate mongoTemplate;


    /**
     * 储存对象 增
     */
    public void save(FlUser flUser){
        System.out.println(mongoTemplate.save(flUser));
    }

    /**
     * 根据用户名查询对象 查
     * @return
     */
    public List<FlUser> findTestByName(String username) {
        Query query=new Query(Criteria.where("username").is(username));
        FlUser flUser =  mongoTemplate.findOne(query , FlUser.class);
        List<FlUser> flUsers = mongoTemplate.find(query, FlUser.class);

        return flUsers;
    }

    /**
     * 根据用户名删除对象 删
     */
    public void deleteTestByName(String username){
        Query query=new Query(Criteria.where("username").is(username));
        System.out.println("是否删除成功"+mongoTemplate.remove(query, FlUser.class));
    }

    /**
     * 根据用户名修改对象  改
     */
    public void updateTestByName(String username,String password){
        Query query=new Query(Criteria.where("username").is(username));
        Update update= new Update().set("username","风离").set("password", password);
        //更新查询返回结果集的第一条
        System.out.println("是否更新成功"+mongoTemplate.updateFirst(query, update, FlUser.class));
        //更新查询返回结果集的所有
        // mongoTemplate.updateMulti(query,update,FlUser.class);

    }


}
